﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the ReusableString model
	/// </summary>
	public partial class ReusableString
	{

		/////////////////////////////////////////////////////////////
		// Static Operations

		public static List<string> Load(DebtChasedEntities ent, string strCategory )
		{

			// Declare return variable
			return (from rs in ent.ReusableStrings
				   where (rs.Category == strCategory)
				   orderby rs.Value
				   select rs.Value).ToList();

		}

		public static void Add(DebtChasedEntities ent, string strCategory, string strValue, bool bAdd)
		{

			// Find the matching id
			ReusableString reusableString = (from rs in ent.ReusableStrings
					  where 
						(rs.Category == strCategory) 
						&& 
						(string.Compare(rs.Value,strValue,true)==0)
					  select rs).FirstOrDefault();

			// if adding...
			if (bAdd)
			{

				// If not already in the database...
				if (reusableString == null)
				{
					reusableString = ReusableString.CreateReusableString(0, strCategory, strValue);
					ent.ReusableStrings.AddObject(reusableString);
					ent.SaveChanges();
				}

			}
			else
			{

				// If id found...
				if (reusableString != null)
				{

					// Delete the record
					ent.DeleteObject(reusableString);
					ent.SaveChanges();

				}

			}

		}

	}

}
